import {DisciplineConsts, DisciplineName, SETTING_PREFIX, TIME_SETTING} from '@/consts/Consts'
import {get, set} from "@/api/persistent";
import {getTime} from "@/utils";

const SETTING = 'setting'

let defaultSetting = {
  log: {
    'count': -1,
    'dungeon': '', // 副本
    'discipline': DisciplineConsts.ZHAN_X, // 职业
    'completed': true, // 完成情况
    'description': '', // 描述
    'image':'',
    'date': '', // 日期
  },
  timeSetting: {
    value: TIME_SETTING.NOW
  },
  excelKey: {
    'count': '完成场次',
    'dungeon': '副本', // 副本
    'discipline': '职业', // 职业
    'completed': '是否完成', // 完成情况
    'description': '描述', // 描述
    'image':'图片',
    'date': '日期', // 日期
    'time': '时间'
  },
  logFormatSettings: {
    disciplineMap: JSON.parse(JSON.stringify(DisciplineName)),
    completed: '完成'
  },
  giteeSetting: {
    access_token: '', // 用户授权码
    owner: '',        // 仓库所属空间地址(企业、组织或个人的地址path)
    repo: '',         // 仓库路径(path)
  }
}

let setting = persistentGet(SETTING) || defaultSetting

export function getDefaultLog() {
  setting.log = setting.log || defaultSetting.log
  setting.log.date = getTime(getTimeSetting().value)
  return setting.log
}

export function getTimeSetting() {
  setting.timeSetting = setting.timeSetting || defaultSetting.timeSetting
  return setting.timeSetting
}

export function getExcelKey() {
  setting.excelKey = setting.excelKey || defaultSetting.excelKey
  return setting.excelKey
}

export function getLogFormatSettings(){
  setting.logFormatSettings = setting.logFormatSettings || defaultSetting.logFormatSettings
  return setting.logFormatSettings
}

export function getGiteeSetting(){
  setting.giteeSetting = setting.giteeSetting || defaultSetting.giteeSetting
  return setting.giteeSetting
}

export function saveSettings(){
  persistentSet(SETTING, setting)
}

function persistentSet(key, value) {
  set(SETTING_PREFIX + key, value)
}

function persistentGet(name) {
  return get(SETTING_PREFIX + name)
}